#!/usr/bin/env python2.4

import sys, os, time, cPickle
from FlickrClient import FlickrClient

def get_pic(sizes, label):
	for size in sizes:
		if size("label") == label:
			return {"url": size("source"),
				"w": size("width"),
				"h": size("height")}

def get_pics(sizes):
	r = {}
	for size in sizes:
		r[size("label")] = {"url": size("source"),
				    "w": size("width"),
				    "h": size("height")}
	return r

client = FlickrClient("c9ebab9a92ec274af792d54cc9a48eb4")

start = time.time()
pics = {}
l = client.flickr_photos_search(user_id="46249124@N00", tags="shoebox", per_page=500)
total = len(l)
i = 0
for pic in l:
	pic_sizes = client.flickr_photos_getSizes(photo_id=pic("id"))
	pic_info = client.flickr_photos_getInfo(photo_id=pic("id"), secret=pic("secret"))
	i += 1
	tags = [str(t) for t in pic_info.tags]
 	r = {"id":pic("id"), "title":pic("title"),
 	     "sizes":get_pics(pic_sizes),
 	     "url":"http://www.flickr.com/photos/razvanm/%s" % (pic("id")),
 	     "comments":str(pic_info.comments),
	     "date":int(pic_info.dates("posted")),
	     "tags":tags,
	     "desc":str(pic_info.description)}
 	pics[pic("id")] = r
	sys.stdout.write("\r%d/%d" % (i, total))
 	sys.stdout.flush()
p = open("shoebox.pickle.new", "w")	
cPickle.dump(pics, p)
p.close()

os.rename("shoebox.pickle.new", "shoebox2.pickle")
print "\nDone (%.2f seconds)" % (time.time() - start)
